package com.zsr.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsr.example.dto.CourseDto;
import com.zsr.example.entity.EduArticle;
import com.zsr.example.entity.EduPath;
import com.zsr.example.entity.TOrder;
import com.zsr.example.entity.TOrderItem;
import com.zsr.example.mapper.IndexMapper;
import com.zsr.example.mapper.TOrderMapper;
import com.zsr.example.service.*;
import com.zsr.example.util.OrderNoUtil;
import com.zsr.example.util.constants.BusinessEnum;
import com.zsr.example.util.constants.OrderConstants;
import com.zsr.example.vo.OrderReq;
import com.zsr.example.vo.TOrderVo;
import com.zsr.example.vo.front.CourseFrontInfoVo;
import com.zsr.example.vo.front.UserInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author zsr
 * @since 2021-12-06
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    @Autowired
    private TOrderItemService orderItemService;


    @Override
    public String saveOrder(Integer targetType, String courseId, String memberIdByJwtToken) {
        //根据课程id获取课程信息
        CourseDto course = this.baseMapper.getCourseDtoByType(targetType, courseId);

        //根据用户id获取用户信息
        UserInfoVo ucenterMember = ucenterMemberService.getLoginInfo(memberIdByJwtToken);
        TOrder order = null;


        order = baseMapper.selectOne(new LambdaQueryWrapper<TOrder>()
                .eq(TOrder::getTargetId, courseId)
                .eq(TOrder::getTargetType, targetType)
                .eq(TOrder::getStatus, OrderConstants.STATUS_NOPAY));

        if (order == null) {
            order = new TOrder()
                    .setOrderNo(OrderNoUtil.getOrderNo())
                    .setTargetType(targetType)
                    .setTargetId(courseId)
                    .setCourseTitle(course.getTitle())
                    .setCourseCover(course.getCover())
                    .setTeacherName(course.getTeacherName())
                    .setTotalFee(course.getPrice())
                    .setMemberId(memberIdByJwtToken)
                    .setNickname(ucenterMember.getNickname())
                    .setEmail(ucenterMember.getEmail())
                    .setStatus(OrderConstants.STATUS_NOPAY);
                    //.setPayType(OrderConstants.PAY_TYPE_WX);
            //TODO 手机号未写入订单
            //order.setMobile(ucenterMember.getMobile());
            baseMapper.insert(order);
        }

        return order.getOrderNo();
    }

    @Override
    public boolean isBuyCourse(String memberId, Integer targetType, String courseId) {
        if (StringUtils.isEmpty(memberId)) return false;
        boolean result;
        int var1 = baseMapper.selectCount(new LambdaQueryWrapper<TOrder>()
                .eq(TOrder::getMemberId, memberId)
                .eq(TOrder::getTargetId, courseId)
                .eq(TOrder::getTargetType, targetType)
                .eq(TOrder::getStatus, OrderConstants.STATUS_PAY));
        if (targetType == BusinessEnum.PATH.getType()) {
            result = var1 > 0;
        } else {
            int var2 = orderItemService.count(new LambdaQueryWrapper<TOrderItem>()
                    .eq(TOrderItem::getMemberId, memberId)
                    .eq(TOrderItem::getTargetId, courseId)
                    .eq(TOrderItem::getTargetType, targetType));
            result = var1 > 0 || var2 > 0;
        }
        return result;
    }

    @Override
    public void pageQuery(Page<TOrder> page, OrderReq orderReq) {
        //MP 构建条件
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        String orderNo = orderReq.getOrderNo();
        String courseId = orderReq.getTargetId();
        Integer targetType = orderReq.getTargetType();
        String TeacherName = orderReq.getTeacherName();
        String memberId = orderReq.getMemberId();
        Integer payType = orderReq.getPayType();
        Integer status = orderReq.getStatus();
        if (!StringUtils.isEmpty(orderNo)) wrapper.eq("order_no", orderNo);//等于
        if (!StringUtils.isEmpty(courseId)) wrapper.eq("target_id", courseId);//等于
        if (!StringUtils.isEmpty(targetType)) wrapper.eq("target_type", targetType);//等于
        if (!StringUtils.isEmpty(TeacherName)) wrapper.like("teacher_name", TeacherName);//等于
        if (!StringUtils.isEmpty(memberId)) wrapper.eq("member_id", memberId);//等于
        if (!StringUtils.isEmpty(payType)) wrapper.eq("pay_type", payType);//等于
        if (!StringUtils.isEmpty(status)) wrapper.eq("status", status);//等于

        baseMapper.selectPage(page, wrapper);
    }

    @Override
    public List<TOrderVo> orderQueryPage(OrderReq orderReq) {
        return baseMapper.orderQueryPage(orderReq);
    }
}
